package com.zws.cucumber.snapshot;

import org.slf4j.Logger;

import java.util.Map;
import java.util.concurrent.ConcurrentMap;

public interface ZwsSnapshotManager<V> {

    String name();

    ConcurrentMap<String, V> snapshots();

    void snapshot0(String snapshotName, Map<String, Object> args);

    default void snapshot(String snapshotName, Map<String, Object> args) {
        long s = System.currentTimeMillis();
        snapshot0(snapshotName, args);
        logger().debug("take snapshot of {}, snapshot name: {}, elapsed: {}ms", name(), snapshotName, System.currentTimeMillis() - s);
    }

    void restore0(String snapshotName, Map<String, Object> args);

    default void restore(String snapshotName, Map<String, Object> args) {
        long s = System.currentTimeMillis();
        restore0(snapshotName, args);
        logger().debug("restore snapshot of {}, snapshot name: {}, elapsed: {}ms", name(), snapshotName, System.currentTimeMillis() - s);
    }

    Logger logger();

    default void delete(String snapshotName) {
        logger().debug("delete snapshot of {}, snapshot name: {},", name(), snapshotName);
        snapshots().remove(snapshotName);
    }

    default void clear() {
        logger().debug("clear snapshot of {}", name());
        snapshots().clear();
    }

    default void put(String snapshotName, V value) {
        if (snapshots().containsKey(snapshotName)) {
            logger().warn("<r>snapshot ''{}'' is existed, replace it with a new one</r>", snapshotName);
        }
        snapshots().put(snapshotName, value);
    }

    default V get(String snapshotName) {
        if (!snapshots().containsKey(snapshotName)) {
            throw new IllegalArgumentException("No Such Snapshot: " + snapshotName);
        }
        return snapshots().get(snapshotName);
    }

}
